package com.ta.consumer;

import com.ta.pojo.Product;
import com.ta.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class orderQueueConsumer {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${product.all}")
    private String productAll;
    @Value("${product.weight}")
    private String productWeight;
    @Value("${product.modifyWeight}")
    private String modifyProductWeight;

    @RabbitListener(queues = "orderQueue")
    public void orderConsumer(Map<String, Object> map){
        System.out.println(map);
        Integer pid = (Integer)map.get("pid");
        /*redis所有商品缓存*/
        Product product = (Product)redisTemplate.opsForHash().get(productAll,pid.toString());

        /*同步redis商品权重缓存*/
        redisTemplate.opsForZSet().incrementScore(productWeight, product, 1);

        /*存储商品权重*/
        Product newProduct = new Product();
        newProduct.setId(pid);
        newProduct.setPriority(redisTemplate.opsForZSet().score(productWeight, product).intValue());
        redisTemplate.opsForHash().put(modifyProductWeight, pid.toString(), newProduct);

        /*支付订单*/
        orderService.paymentOrder(map);
    }
}
